﻿using System.Collections.ObjectModel;
using Matrix.Xmpp.Client;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using System;
using System.Linq;
using System.Threading.Tasks;
using Matrix.Xmpp.Vcard;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace MatriXSamples
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        XmppClient client;
        ObservableCollection<Contact> contacts = new ObservableCollection<Contact>();

        public MainPage()
        {
            this.InitializeComponent();
            this.DataContext = contacts;
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            client = e.Parameter as XmppClient;
            client.Dispatcher = Dispatcher;
            //client.OnLogin += client_OnLogin;
            //client.OnPresence += client_OnPresence;
            client.OnRosterItem += client_OnRosterItem;
            //client.OnError += client_OnError;
        }

        //private void Button_Click_1(object sender, RoutedEventArgs e)
        //{
        //    //client = new XmppClient
        //    //{

        //    //    Username = "if05041",

        //    //    Password = "joe@satriani41",

        //    //    XmppDomain = "gmail.com",

        //    //    Hostname = "talk.google.com"
        //    //};

        //    //client.OnLogin += client_OnLogin;
        //    //client.OnPresence += client_OnPresence;
        //    //client.OnRosterItem += client_OnRosterItem;
        //    //client.OnError += client_OnError;
        //    //client.Open();
        //}

        async void client_OnRosterItem(object sender, Matrix.Xmpp.Roster.RosterEventArgs e)
        {
            if (e.RosterItem == null) return;
            if (e.RosterItem.Name == null) return;

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => 
            {
                contacts.Add(new Contact { Id = e.RosterItem.Jid, Name = e.RosterItem.Name });
            });

            client.IqFilter.SendIq(new VcardIq { To = e.RosterItem.Jid, Type = Matrix.Xmpp.IqType.get }, (o, ev) =>
            {
                var vcard = ev.Iq.Query as Vcard;
                if (vcard == null) return;
                if (vcard.Photo == null) return;

                var photos = vcard.Photo.Binval;

                var contact = contacts.Where(x => x.Id.Bare == ev.Iq.From.Bare).FirstOrDefault();

                if (contact == null) return;

                Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    contact.Avatar = photos.ToBitmap();
                });
            });
            
            //Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => 
            //{
            //    if (e.RosterItem == null) return;
            //    if (e.RosterItem.Name == null) return;

            //    lstRoster.Items.Add(e.RosterItem.Name);
            //});
        }

        //void client_OnError(object sender, Matrix.ExceptionEventArgs e)
        //{

        //}

        //void client_OnLogin(object sender, Matrix.EventArgs e)
        //{

        //}

        //void client_OnPresence(object sender, PresenceEventArgs e)
        //{

        //}

        private void ImageOverlayGalleryFolderGrid_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Frame.Navigate(typeof(ChatWindow), new ChatFriendData { Client = client, Contact = (Contact)e.ClickedItem });
        }
    }
}
